/**
 * 
 */
package com.demo2do.boyle.backend.web.view;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.view.AbstractView;

import com.demo2do.core.utils.StringUtils;

import net.sf.jxls.transformer.XLSTransformer;

/**
 * 
 * @author David
 */
public class ExcelTemplateView extends AbstractView {

	/** The content type for an Excel response */
	private static final String CONTENT_TYPE = "application/vnd.ms-excel";

	private static final String CONTENT_DISPOSITION_PREFIX = "attachment;";

	private static final String DEFAULT_FILE_NAME = "download.xls";

	private String templateXLSFile;

	private String fileName;

	private static final Log logger = LogFactory.getLog(ExcelTemplateView.class);

	/**
	 * 
	 * @param templateXLSFile
	 */
	public ExcelTemplateView(String templateXLSFile) {
		super.setContentType(CONTENT_TYPE);
		this.templateXLSFile = templateXLSFile;
		this.fileName = templateXLSFile;
	}

	/**
	 * 
	 */
	public ExcelTemplateView(String templateXLSFile, String fileName) {
		super.setContentType(CONTENT_TYPE);
		this.templateXLSFile = templateXLSFile;
		this.fileName = fileName;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * 
	 * @return
	 */
	public String getContentDisposition() {
		String fileName = StringUtils.isBlank(this.fileName) ? DEFAULT_FILE_NAME : this.fileName;
		return CONTENT_DISPOSITION_PREFIX + " filename=" + fileName;
	}

	/* 
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

		logger.info("Start exporting excel file: " + this.templateXLSFile);
		File template = ResourceUtils.getFile("classpath:template/" + this.templateXLSFile);

		// generate workbook from template file
		XLSTransformer transformer = new XLSTransformer();
		Workbook workbook = transformer.transformXLS(new FileInputStream(template), model);

		response.setHeader("Content-Disposition", getContentDisposition());
		// no cache here
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expire", 0); 
		response.setContentType(super.getContentType());

		ServletOutputStream outputStream = response.getOutputStream();
		
		workbook.write(outputStream);
		outputStream.flush();
		logger.info("End exporting excel file: " + this.templateXLSFile);

	}

}
